from selenium import webdriver
from selenium.webdriver.support.select import Select
import time
import xlrd


# 登陆网站
def zhtj_login():
    drivers = webdriver.Chrome()
    drivers.get('https://zhtj.youth.cn/zhtj/')
    user_id = '410527198004140036'
    user_pw = '227718Lk'
    drivers.find_element_by_id('loginId').send_keys(user_id)
    drivers.find_element_by_id('password').send_keys(user_pw)
    user_yzm = input('Please input your yanzhengma:')
    drivers.find_element_by_id('yzm').send_keys(user_yzm)
    drivers.find_element_by_id('subBut').click()
    return drivers


# 进入添加成员界面
def enter_append_gui(drivers):
    # drivers.find_element_by_xpath("/html/body/div/div/div[2]/div[1]/div/div[2]/ul/li[6]").click()
    drivers.find_element_by_xpath('/html/body/div/div/div[2]/div[1]/div/div[2]/ul/li[9]/div').click()
    time.sleep(5)
    # print(drivers.find_element_by_class_name("el-menu-item"))
    # drivers.find_elements_by_class_name("el-menu-item")[0].click()
    drivers.find_element_by_xpath("/html/body/div/div/div[2]/div[1]/div/div[2]/ul/li[9]/ul/li[1]").click()
    # drivers.find_element_by_xpath("/html/body/div/div/div[2]/div[1]/div/div[2]/ul/li[6]/ul/li[1]").click()
    time.sleep(2)
    drivers.find_element_by_id('tab-first').click()
    time.sleep(2)
    drivers.find_element_by_xpath(
        '/html/body/div/div/div[2]/div[2]/div/div[2]/div[1]/div[2]/div[2]/div[1]/button').click()
    time.sleep(2)
    return drivers


# 选择添加团支部
def selected_class(drivers, classes):
    # 这个写法不太好，会把程序写死，不能进行自动选择
    # drivers.find_element_by_xpath("/html/body/div/div/div[2]/div[2]/div/div[2]/div[1]/div[1]/ul/li[2]/div/ul[1]/li[1]/a/span/span[2]").click()
    # time.sleep(5)
    # drivers.find_element_by_css_selector("#orgtreeDiv > div.el-tree.ztree.ztree.el-tree--highlight-current > ul > li:nth-child(2) > div > ul:nth-child(2) > li.el-tree-node__content > a > span > span.node_name").click()
    # 这个方法好可以让程序根据实际情况自己选择
    drivers.find_element_by_css_selector("a[title=%s" % (classes)).click()
    return drivers


# 读取个人信息
def get_mem_info(drivers):
    #数据存放位置
    db = xlrd.open_workbook("/home/m/高一新生团员信息统计.xls")
    # 选中数据表
    table_info = db.sheet_by_name('Sheet1')
    print(table_info.name)
    # 逐行读取团员信息
    for i in range(14, table_info.nrows):
        print(table_info.row_values(i))
        mem_info_list = table_info.row_values(i)
        # 团员编号
        ty_name = mem_info_list[1].strip()
        ty_sfzh = mem_info_list[2].strip()
        time.sleep(2)
        #查询团员信息异常处理
        # / html / body / div[2] / p
        # print('before',drivers.find_element_by_xpath('/html/body/div[2]/p').text)
        drivers.find_element_by_xpath('/html/body/div[1]/div/div[2]/div[2]/div/div[2]/'
                                      'div/div[2]/form/table/tr[1]/td[2]/div/div/div/input').send_keys('李飞')
        drivers.find_element_by_xpath('/html/body/div[1]/div/div[2]/div[2]/div/div[2]/'
                                      'div/div[2]/form/table/tr[2]/td[2]/div/div/div/input').send_keys('410527198804140036')
        time.sleep(0.5)
        drivers.find_element_by_xpath('/html/body/div[1]/div/div[2]/div[2]/div/div[2]/div/div[2]/div/button[2]').click()

        # print('----' * 4, drivers.switch_to_alert())

        # # 转入原因
        # time.sleep(3)
        # drivers.find_element_by_id('inReson').click()
        # time.sleep(1)
        # drivers.find_elements_by_xpath('/html/body/div[2]/div[1]/div[1]/ul/li')[0].click()
        # time.sleep(3)
        # # 部分团员在村支部，不存在毕业去向等问题，所以需异常处理
        # try:
        #     # 毕业去向
        #     drivers.find_element_by_id('whereToGraduate').click()
        #     time.sleep(1)
        #     drivers.find_elements_by_xpath('/html/body/div[3]/div[1]/div[1]/ul/li')[0].click()
        #     time.sleep(1)
        #     # 新学校名称
        #     drivers.find_element_by_id('unitname').send_keys('内黄县第二高级中学')
        #     time.sleep(0.5)
        #     # 新学校详细地址
        #     # 详细地址，弹出框
        #     drivers.find_element_by_xpath('/html/body/div[1]/div/div[2]/div[2]/div/'
        #                                   'div[2]/div/div[2]/table/tbody[3]/tr[2]/td[2]/div/input').click()
        #     time.sleep(1)
        #     # 省市县
        #     drivers.find_element_by_xpath('/html/body/div[1]/div/div[2]/div[2]/div/'
        #                                   'div[3]/div/div[2]/table/tr[1]/td[2]/div/div[1]/input').click()
        #     time.sleep(1)
        #     # 省
        #     drivers.find_elements_by_xpath('/html/body/div[5]/div[1]/div/div[1]/ul/li')[15].click()
        #     time.sleep(1)
        #     # 市
        #     drivers.find_elements_by_xpath('/html/body/div[5]/div[1]/div[2]/div[1]/ul/li')[4].click()
        #     time.sleep(1)
        #     # 县
        #     drivers.find_elements_by_xpath('/html/body/div[5]/div[1]/div[3]/div[1]/ul/li')[7].click()
        #     time.sleep(1)
        #     # 具体地址
        #     drivers.find_element_by_id('street').send_keys('西环路')
        #     time.sleep(1)
        #     # 确定
        #     drivers.find_element_by_xpath(
        #         '/html/body/div[1]/div/div[2]/div[2]/div/div[3]/div/div[3]/span/button[2]').click()
        # except:
        #     pass
        #
        # # 提交
        # drivers.find_element_by_xpath('/html/body/div[1]/div/div[2]/div[2]/div/div[2]/div/div[2]/div/button[2]').click()
        # # 进入办理转入菜单
        # time.sleep(8)
        # drivers.find_element_by_xpath(
        #     '/html/body/div/div/div[2]/div[2]/div/div[2]/div[1]/div[2]/div[2]/div[1]/button').click()

        time.sleep(3)


# 添加个人信息并提交
def append_info(drivers):
    pass


if __name__ == '__main__':
    driver = zhtj_login()
    time.sleep(3)
    dirver = enter_append_gui(driver)
    time.sleep(3)
    mem_info = get_mem_info(dirver)
    # classes='河南省安阳市内黄县第二高级中学2020级学生团支部'
